package cn.iocoder.yudao.module.call.controller.admin.voicefile;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;

import cn.iocoder.yudao.module.call.controller.admin.voicefile.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.voicefile.VoiceFileDO;
import cn.iocoder.yudao.module.call.service.voicefile.VoiceFileService;

@Tag(name = "管理后台 - 呼叫中心语音文件")
@RestController
@RequestMapping("/call/voice-file")
@Validated
public class VoiceFileController {

    @Resource
    private VoiceFileService voiceFileService;

    @PostMapping("/create")
    @Operation(summary = "创建呼叫中心语音文件")
    public CommonResult<Long> createVoiceFile(@Valid VoiceFileSaveReqVO createReqVO){
        return success(voiceFileService.createVoiceFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新呼叫中心语音文件")
    public CommonResult<Boolean> updateVoiceFile(@Valid @RequestBody VoiceFileSaveReqVO updateReqVO) {
        voiceFileService.updateVoiceFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除呼叫中心语音文件")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteVoiceFile(@RequestParam("id") Long id) {
        voiceFileService.deleteVoiceFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得呼叫中心语音文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<VoiceFileRespVO> getVoiceFile(@RequestParam("id") Long id) {
        VoiceFileDO voiceFile = voiceFileService.getVoiceFile(id);
        return success(BeanUtils.toBean(voiceFile, VoiceFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得呼叫中心语音文件分页")
    public CommonResult<PageResult<VoiceFileRespVO>> getVoiceFilePage(@Valid VoiceFilePageReqVO pageReqVO) {
        PageResult<VoiceFileDO> pageResult = voiceFileService.getVoiceFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoiceFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出呼叫中心语音文件 Excel")
    @OperateLog(type = EXPORT)
    public void exportVoiceFileExcel(@Valid VoiceFilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoiceFileDO> list = voiceFileService.getVoiceFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "呼叫中心语音文件.xls", "数据", VoiceFileRespVO.class,
                        BeanUtils.toBean(list, VoiceFileRespVO.class));
    }

}